package control
{
	import com.yzy.models.SetLibs;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import effect.Effect;
	
	import event.ControlEvent;
	
	import game.bejeweled.BackDrop;
	import game.bejeweled.BejeweledBlock;
	import game.bejeweled.CreateBalls;
	import game.monster.Monster;
	import game.monster.MonstersBlock;
	import game.ui.GameUI;
	import game.ui.NumberMC;
	
	import models.ProfileModel;
	
	import starling.textures.Texture;
	

	public class Control extends flash.events.EventDispatcher
	{
		public function Control() {}
		
		private const timerNum:int=1;
		private var gameTimer:Timer;
		private var bjBlock:BejeweledBlock=BejeweledBlock.instant;
		private var MnBlock:MonstersBlock=MonstersBlock.instant;
		private var rfBlock:Effect=Effect.instant;
		private var gameui:GameUI=GameUI.instant;
		private var createBall:CreateBalls;

		private var batterTotal:int;
		public function initGameCtrl():void{
			//
			ProfileModel.instant.AllowCtrl=false;
			//run Timer
			runTimer();
			//control game
			controlGame();
		}
		
		private function controlGame():void{
			createBall=new CreateBalls(bjBlock,bjBlock.backdropTexture);
			createBall.addEventListener(ControlEvent.CREATE_BALL_COMPLETE,cretaeBallComplete);
			this.addEventListener(ControlEvent.REMOVE_BALL_COMPLETE,function createball():void{
				createBall.createBall();
			});
			
			createBall.createBall();
		}
		
		private function cretaeBallComplete(evt:ControlEvent):void{
			var sameBalls:Vector.<Point>=createBall.sameBall();
			
			if(sameBalls.length){
				ProfileModel.instant.AllowCtrl=false;
				batter(true,sameBalls[sameBalls.length>>1]);
				removeBall(sameBalls);
			}else{
				ProfileModel.instant.AllowCtrl=true;
				batter(false);
			}

		}
		
		
		/**
		 * 删除ball 
		 * @param balls
		 */		
		private function removeBall(balls:Vector.<Point>):void{
			for each(var ballPoint:Point in balls){
				var currBallback:BackDrop=ProfileModel.instant.backdropVec[ballPoint.x][ballPoint.y];
				var ms:Monster=ProfileModel.instant.monsterVec[0];
				var p:Point=ms.localToGlobal(new Point(0,0));
				var p2:Point=bjBlock.globalToLocal(p);
				Effect.instant.shootBallEff(currBallback,p2);
				//
				addScore(currBallback);
			}
			Effect.instant.addEventListener("removeball_complete",removeComplete);
		}
		
		/**
		 *  连击效果 
		 */		
		private function batter(isbatter:Boolean,place:Point=null):void{
			if(isbatter){
				batterTotal++;
			}else{
				batterTotal=0;
			}
			
			if(batterTotal>1){
//				trace("连击X"+batterTotal);
				var backdrop:BackDrop=ProfileModel.instant.backdropVec[place.x][place.y];
				place=backdrop.localToGlobal(new Point(0,0));
				gameui.setMultiple(batterTotal,place);
			}
		}
		
		
		/**
		 * 算分 
		 * @param ballback
		 * 
		 */		
		private function addScore(ballback:BackDrop):void{
			GameUI.instant.setScore(ballback.currBall.ballProfile.degree);
		}
		
		
		private function removeComplete():void
		{
			
			
			//trace("消除完成")
			this.dispatchEvent(new ControlEvent(ControlEvent.REMOVE_BALL_COMPLETE));
		}		
		
		
		private function runTimer(run:Boolean=true):void{
			if(run){
				gameTimer=new Timer(timerNum);
				gameTimer.addEventListener(TimerEvent.TIMER,runGame);
				gameTimer.start();
			}else{
				gameTimer.stop();
				gameTimer.removeEventListener(TimerEvent.TIMER,runGame);
			}	
		}
		
		private function runGame(evt:TimerEvent):void
		{
			if(ProfileModel.instant._runVec.length>0){
				for (var j:int = ProfileModel.instant._runVec.length-1; j >=0; j--) 
				{
					if(ProfileModel.instant._runVec[j]){
						ProfileModel.instant._runVec[j].Run(j);
					}
				}
			}else{
				runTimer(false);
			}		
		}
		
		
		
		public function setRunVec(value:IRunCtrl):void
		{
			if(ProfileModel.instant._runVec.length==0){
				runTimer(true);
			}
			ProfileModel.instant._runVec.push(value);
			
		}
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:Control;
		public static function get instant():Control
		{
			if(null == _instant)
			{
				_instant =  new Control();
			}
			return _instant;
		}
	}
}